package com.dian.test;

import com.alibaba.fastjson.JSON;

import java.util.regex.Pattern;

public class MergeSort {

    //将有序数组a[]和b[]合并到c[]中
    void MemeryArray(int[] a, int n, int[] b, int m, int[] c)
    {
        int i, j, k;

        i = j = k = 0;
        while (i < n && j < m)
        {
            if (a[i] < b[j])
                c[k++] = a[i++];
            else
                c[k++] = b[j++];
        }

        while (i < n)
            c[k++] = a[i++];

        while (j < m)
            c[k++] = b[j++];
    }

    void mergearray(int[] a, int first, int mid, int last, int[] temp)
    {
        int i = first, j = mid + 1;
        int m = mid,   n = last;
        int k = 0;

        while (i <= m && j <= n)
        {
            if (a[i] <= a[j])
                temp[k++] = a[i++];
            else
                temp[k++] = a[j++];
        }

        while (i <= m)
            temp[k++] = a[i++];

        while (j <= n)
            temp[k++] = a[j++];

        for (i = 0; i < k; i++)
            a[first + i] = temp[i];
    }

    void mergesort(int[] a, int first, int last, int[] temp)
    {
        if (first < last)
        {
            int mid = (first + last) / 2;
            mergesort(a, first, mid, temp);    //左边有序
            mergesort(a, mid + 1, last, temp); //右边有序
            mergearray(a, first, mid, last, temp); //再将二个有序数列合并
        }
    }

    boolean MergeSort(int[] a, int n)
    {
        int[] p = new int[n];
        if (p == null)
            return false;
        mergesort(a, 0, n - 1, p);
        return true;
    }


    public static void main(String[] args) {
        MergeSort mergeSort = new MergeSort();
        int[] a = {100,34,5,7,9,11,12,13};
        int[] b = {2,3,6,8,10,32,43,0,2};

        int[] c = new int[a.length + b.length];

        mergeSort.MemeryArray(a, a.length, b, b.length, c);

        System.out.println(JSON.toJSONString(c));

        int[] arr = {11, 3, 1, 4, 1, 2, 20, 5, 10, 6};

        mergeSort.MergeSort(arr, arr.length);

       // System.out.println(JSON.toJSONString(arr));
        System.out.println("xxxx:");
        String content = "ACT_ID_GROUP";
        String pattern = "ACT_*";
        boolean isMatch = Pattern.matches(pattern, content);
        System.out.println("xx:"+isMatch);
    }
}
